package com.sojpt.boot.controller.business;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.sojpt.boot.controller.business.validator.LkyCollegeAddValidator;
import com.sojpt.boot.controller.business.validator.LkyCollegeUpdateValidator;
import com.sojpt.boot.intercepter.admin.AdminLoginInterceptor;
import com.sojpt.boot.intercepter.admin.AdminPopedomInterceptor;
import com.sojpt.boot.ui.iview.IViewController;
import com.sojpt.kit.SoJLogKit;
import com.sojpt.model.DicPopedom;
import com.sojpt.model.LkyCollege;
import com.sojpt.util.ResultUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 *
 * @author Sohnny
 *
 */
@RestController
@RequestMapping("/admin/college/")
//@Before({AdminLoginInterceptor.class, AdminPopedomInterceptor.class})
public class LkyCollegeAdminController extends IViewController {


    @GetMapping("list")
    public void list() {
        LkyCollege lkyCollege = getModel(LkyCollege.class, "lkyCollege");
        lkyCollege.put("creation_time_start_time", getPara("creation_time_start_time"));
        lkyCollege.put("creation_time_end_time", getPara("creation_time_end_time"));
        lkyCollege.put("update_time_start_time", getPara("update_time_start_time"));
        lkyCollege.put("update_time_end_time", getPara("update_time_end_time"));
        SqlPara sqlPara = Db.getSqlPara("lky_college.select", getKv(lkyCollege));
        Page<Record> page = Db.paginate(pageNumber(), pageSize(), sqlPara);
        renderJson(page);
    }

    @GetMapping("queryAll")
    public void queryAll() {
        LkyCollege lkyCollege = getModel(LkyCollege.class, "lkyCollege");
        SqlPara sqlPara = Db.getSqlPara("lky_college.select", getKv(lkyCollege));
        List<Record> records = Db.find(sqlPara);
        renderJson(ResultUtil.success(records));
    }


    @Before(LkyCollegeAddValidator.class)
    @PostMapping("save")
    public void save() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/college/save");
        LkyCollege lkyCollege = getModel(LkyCollege.class, "lkyCollege");
        lkyCollege.setId(UUID.randomUUID().toString().replace("-", ""));
        lkyCollege.setCreationTime(new Date());
        lkyCollege.setUpdateTime(new Date());
        if(lkyCollege.save()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("添加成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("添加失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @Before(LkyCollegeUpdateValidator.class)
    @PostMapping("update")
    public void update() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/college/update");
        LkyCollege lkyCollege = getModel(LkyCollege.class, "lkyCollege");
        lkyCollege.setUpdateTime(new Date());
        if(lkyCollege.update()) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("修改成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("修改失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("delete")
    public void delete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/college/delete");
        LkyCollege lkyCollege = getModel(LkyCollege.class, "lkyCollege");
        String id=lkyCollege.getId();
        String mid = Db.queryStr("select count(*) from lky_major where coid = ?", id);
        int a = Integer.parseInt(mid);
        if(a>0){
            renderJson(false);
            dicPopedom.setPopedomName("删除失败");
        }else {
            if (LkyCollege.dao.deleteById(lkyCollege.getId())) {
                dicPopedom.setPopedomName("删除成功");
                renderJson(true);
            } else {
                dicPopedom.setPopedomName("删除失败");
                renderJson(false);
            }
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

    @PostMapping("batchDelete")
    public void batchDelete() {
        DicPopedom dicPopedom = new DicPopedom();
        dicPopedom.setForwardAction("/admin/college/batchDelete");
        String[] ids = getPara("ids").split(",");
        SqlPara sqlPara = Db.getSqlPara("lky_college.deleteByIds", Kv.by("idList", Arrays.asList(ids)));
        if(Db.update(sqlPara) > 0) {
            renderJson(Ret.ok());
            dicPopedom.setPopedomName("批量删除成功");
        } else {
            renderJson(Ret.fail());
            dicPopedom.setPopedomName("批量删除失败");
        }
        SoJLogKit.writeLog(this, dicPopedom,getSessionUser());
    }

}